package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.ilovecode.events.MenuEvent;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class MainMenuBusiness extends View {
		
		//{ region Stage elements
		
		public var aboutProjectBusinessButton:SimpleButton;
		public var businessButton:SimpleButton;
		public var garageButton:SimpleButton;
		public var contactBusinessButton:SimpleButton;
		public var businessMenu:BusinessMenu;
		public var businessExtendedMenu:BusinessExtendedMenu;
		
		//} endregion
		
		private var timer:Timer;
		private var holdTimer:Timer;
		private var isMouseOut:Boolean = false;
		private var _mode:String = "";
		
		public function MainMenuBusiness() {
			//this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			super("mainMenuBusiness");
		}
		
		override protected function initUI():void
		{
			super.initUI();
			businessMenu.initMenuExtended(businessExtendedMenu);
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			businessButton.addEventListener(MouseEvent.MOUSE_OVER, businessButtonMouseOver);
			businessButton.addEventListener(MouseEvent.MOUSE_OUT, businessButtonMouseOut);
			businessButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			businessExtendedMenu.addEventListener(MenuEvent.CLICK, businessExtendedMenuClick);
			businessMenu.addEventListener(MenuEvent.CLICK, businessMenuClick);
			aboutProjectBusinessButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			garageButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			contactBusinessButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			businessButton.removeEventListener(MouseEvent.MOUSE_OVER, businessButtonMouseOver);
			businessButton.removeEventListener(MouseEvent.MOUSE_OUT, businessButtonMouseOut);
			businessButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			businessExtendedMenu.removeEventListener(MenuEvent.CLICK, businessExtendedMenuClick);
			businessMenu.removeEventListener(MenuEvent.CLICK, businessMenuClick);
			aboutProjectBusinessButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			garageButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			contactBusinessButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
		}
		
		/*
		private function addedToStage(event:Event):void {
			init();
		}
		
		private function init():void {
			initListeners();
			businessMenu.initMenuExtended(businessExtendedMenu);
		}
		
		private function initListeners():void {
			businessButton.addEventListener(MouseEvent.MOUSE_OVER, businessButtonMouseOver);
			businessButton.addEventListener(MouseEvent.MOUSE_OUT, businessButtonMouseOut);
			businessButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			businessExtendedMenu.addEventListener(MenuEvent.CLICK, businessExtendedMenuClick);
			businessMenu.addEventListener(MenuEvent.CLICK, businessMenuClick);
			aboutProjectBusinessButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			garageButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			contactBusinessButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
		}*/
		
		private function businessExtendedMenuClick(event:MenuEvent):void {
			//this.dispatchEvent(event);
			var screenName:String;
			switch(event.item)
			{
				case "shop":
					screenName = ScreenName.LAYOUT_SHOP;
					break;
				case "catering":
					screenName = ScreenName.LAYOUT_CATERING;
					break;
				case "kindergarten":
					screenName = ScreenName.LAYOUT_KINDERGARTEN;
					break;
				default:
					break;
			}
			controller.showScreen(screenName);
		}
		
		private function menuButtonClick(event:MouseEvent):void {
			var name:String = String(event.currentTarget.name).replace("Button", "");
			//this.dispatchEvent(new MenuEvent(MenuEvent.CLICK, name));
			var screenName:String;
			switch(name)
			{
				case "business":
					screenName = ScreenName.RENDER;
					break;
				case "aboutProjectBusiness":
					screenName = ScreenName.ABOUT_PROJECT;
					break;
				case "garage":
					screenName = ScreenName.GARAGE;
					break;
				case "contactBusiness":
					screenName = ScreenName.CONTACT;
					break;
				default:
					break;
			}
			controller.showScreen(screenName);
		}
		
		private function businessMenuClick(event:MenuEvent):void {
			//this.dispatchEvent(event);
			var screenName:String;
			switch(event.item)
			{
				case "rent":
					screenName = ScreenName.LAYOUT_RENT;
					break;
				case "businessDescription":
					screenName = ScreenName.ABOUT;
					break;
				default:
					break;
			}
			controller.showScreen(screenName);
		}
		
		//{ region Show business menu
		
		private function businessButtonMouseOver(event:MouseEvent):void {
			businessMenu.show();
		}
		
		private function businessButtonMouseOut(event:MouseEvent):void {
			timer = new Timer(500, 1);
			timer.addEventListener(TimerEvent.TIMER, businessTimerTick);
			timer.start();
		}
		
		private function businessTimerTick(event:TimerEvent):void {
			if (!businessMenu.isActive) {
				businessMenu.hide();
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, businessTimerTick);
			}
		}
		
		//} endregion
		
	}

}